#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2020/3/17  14:03
# @Author: 余浪人
# @email: yulangren520@gmail.com
from apps.control import sys_bp
from flask import request, render_template, jsonify
from flask_login import login_required
from apps.forms.user_form import TL_User_Form, TL_User_Edit_Form, TL_Grade_Form
from apps.models.user_model import TL_User_Model, db, TL_Grade_Model
from apps.lib.public import add_data, check_obj, edit_data, real_delete_data, real_deletes, page_auto


@sys_bp.route('/user/user_list', endpoint='user_list', methods=['GET', 'DELETE', 'DELETES', 'UPDATE'])
@login_required
def user_list():
    if request.method == 'DELETE':
        return real_delete_data(TL_User_Model, db, request.form.get('uid'))
    elif request.method == 'DELETES':
        return real_deletes(TL_User_Model, db, request.form.get('id_array').split('#TL#'))
    elif request.method == 'UPDATE':
        data_type, id_list = request.form.get('datatype'), [i for i in request.form.get('id_array').split('#TL#') if i != 'on']
        if data_type == 'onfree':
            condition = {'status': 1}
        elif data_type == 'oncomment':
            condition = {'comment_jurisdiction': 1}
        elif data_type == 'oncontent':
            condition = {'content_jurisdiction': 1}
        elif data_type == 'unfree':
            condition = {'status': 0}
        elif data_type == 'uncomment':
            condition = {'comment_jurisdiction': 0}
        elif data_type == 'uncontent':
            condition = {'content_jurisdiction': 0}
        else:
            return jsonify({'status': 1, 'msg': '系统未正常识别操作行为！取消操作'})
        for uid in id_list:
            user = TL_User_Model.query.filter_by(id=uid).first()
            if user: user.set_attrs(condition)
        try:
            db.session.commit()
        except:
            db.session.rollback()
            return jsonify({'status': 1, 'msg': '修改部分失败！取消'})
        return jsonify({'status': 0})
    search_model, search = request.args.get('search_model', default=0), request.args.get('search', '')
    if search and search_model:
        try:
            if int(search_model) == 1:
                user_list = TL_User_Model.query.filter(TL_User_Model.username == search)
            elif int(search_model) == 2:
                user_list = TL_User_Model.query.filter(TL_User_Model.user_qq == search)
            else:
                user_list = TL_User_Model.query.order_by(TL_User_Model.id.desc())
        except:
            user_list = TL_User_Model.query.order_by(TL_User_Model.id.desc())
    else:
        user_list = TL_User_Model.query.order_by(TL_User_Model.id.desc())
    data_list, page = page_auto(user_list)
    return render_template('system_template/user/user_list.html', data_list=data_list, page=page)


@sys_bp.route('/user/user_add', endpoint='user_add', methods=['GET', 'POST'])
@login_required
def user_add():
    if request.method == 'POST':
        return add_data(TL_User_Model, TL_User_Form, db)
    return render_template('system_template/user/user_add.html', form=TL_User_Form(request.form))


@sys_bp.route('/user/user_edit/<int:uid>', endpoint='user_edit', methods=['GET', 'POST'])
@login_required
def user_edit(uid):
    user = TL_User_Model.query.filter_by(id=uid).first()
    if not check_obj(user): pass
    if request.method == 'POST': return edit_data(TL_User_Edit_Form, TL_User_Model, db, uid)
    return render_template('system_template/user/user_add.html', form=TL_User_Edit_Form(data=dict(user)))


@sys_bp.route('/user/user_grade', endpoint='user_grade', methods=['GET', 'UPDATE', 'DELETE', 'ADDITION'])
@login_required
def user_grade():
    if request.method == 'DELETE':
        return real_delete_data(TL_Grade_Model, db, request.form.get('uid'))
    elif request.method == 'ADDITION':
        return add_data(TL_Grade_Model, TL_Grade_Form, db)
    elif request.method == 'UPDATE':
        uid = request.form.get('data')
        grade_name = request.form.get('grade_name')
        grade_integral = request.form.get('grade_integral')
        try:
            obj = TL_Grade_Model.query.filter_by(id=uid).first()
            if not check_obj(obj): pass
            obj.set_attrs({'grade_name': grade_name, 'grade_integral': grade_integral})
            db.session.commit()
        except:
            db.session.rollback()
            return jsonify({'status': 1, 'msg': '修改异常'})
        return jsonify({"status": 0, 'msg': '修改成功'})
    data_list = TL_Grade_Model.query.order_by(TL_Grade_Model.id.desc()).all()
    return render_template('system_template/user/user_grade.html', data_list=data_list)
